﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;

using SharedGenomics.Core;
using SharedGenomics.DataAccess;
using SharedGenomics.Workbench.Core;
using SharedGenomics.Workbench.Models;
using SharedGenomics.Workbench.PresentationModel;

namespace SharedGenomics.Workbench.Views.WindowsForms
{
    public partial class OpenGenotypeFileView : View, IOpenGenotypeFileView
    {
        public OpenGenotypeFileView()
        {
            InitializeComponent();

            this.lstMissingMarkers.SelectedIndexChanged += (o, e) => { this.SelectedMissingMarkerChanged.Raise(this); };
            this.btLookupMissingMarkers.Click += (o, e) => { this.LookupMissingMarkersClicked.Raise(this); };
        }

        [Region("FileBrowse")]
        public Panel FileBrowsePanel
        {
            get { return this.plOpenFile; }
        }

        [Region("MarkerEdit")]
        public Panel MarkerEditPanel
        {
            get { return this.plEditMarker; }
        }

        [Region("WatchList")]
        public Panel WatchListPanel
        {
            get { return this.plWatchList; }
        }

        public IMissingMarkersModel Model
        {
            set
            {
                value.ThrowIfNull("Model");
                value.ListChanged += (o, e) => { this.SetMissingMarkerNames((IMissingMarkersModel)o); };
                this.SetMissingMarkerNames(value);
            }
        }

        private void SetMissingMarkerNames(IMissingMarkersModel model)
        {
            Debug.Assert(model != null);
            this.lstMissingMarkers.Items.Clear();
            this.lstMissingMarkers.Items.AddRange(model.Items.ToArray());
        }

        public string SelectedMissingMarker
        {
            get { return this.lstMissingMarkers.SelectedItem as string; }
        }

        public event EventHandler SelectedMissingMarkerChanged;

        public int MarkerCount
        {
            set { this.txtMarkerCount.Text = value.ToString(); }
        }

        public int IndividualCount
        {
            set { this.txtIndividualCount.Text = value.ToString(); }
        }

        public bool IsLookupMissingMarkersEnabled
        {
            set { this.btLookupMissingMarkers.Enabled = value; }
        }

        public event EventHandler LookupMissingMarkersClicked;
        public event EventHandler NextClicked;

        public IEnumerable<ValueDisplayItem<GenotypeDataFileFormat>> FileFormats
        {
            set { this.fileFormatsBindingSource.DataSource = value; }
        }

        public GenotypeDataFileFormat SelectedFileFormat
        {
            get { return (GenotypeDataFileFormat)this.cmbFileFormats.SelectedValue; }
        }

        private void btNext_Click(object sender, EventArgs e)
        {
            this.NextClicked.Raise(this);
        }

        public bool IsMovedNextEnabled
        {
            set { this.btNext.Enabled = value; }
        }
    }
}
